﻿using LiveChartsCore;
using LiveChartsCore.Defaults;
using LiveChartsCore.SkiaSharpView;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace DeviceController.Wpf.ViewModels
{
    internal class ThicknessChartViewModel
    {
        public string Title { get; set; }


        /// <summary>
        /// 初始化厚度数据图标。
        /// </summary>
        public ThicknessChartViewModel()
        {
            ObservableCollection<ObservablePoint> values = new ObservableCollection<ObservablePoint>();
            Title = "厚度数据曲线图";
            var trend = 1000;
            var r = new Random();

            for (var i = 0; i < 500; i++)
            {
                values.Add(new ObservablePoint(i, trend += r.Next(-20, 20)));
            }

            Series = new ISeries[]
            {
                new LineSeries<ObservablePoint>
                {
                    Values = values,
                    GeometryStroke = null,
                    GeometryFill = null,
                    DataPadding = new(0, 1),
                }
            };
            ScrollableAxes = new[] { new Axis() };
        }

        public ISeries[] Series { get; set; }

        public Axis[] ScrollableAxes { get; set; }

        public void SetNewLine(List<float> values)
        {
            var lineValues = new ObservableCollection<ObservablePoint>();

            for (int index = 0; index < values.Count; index++)
            {
                lineValues.Add(new ObservablePoint(index, values[index]));
            }

            Application.Current.Dispatcher.Invoke(() =>
            {
                Series.First().Values = lineValues;
            });
        }
    }
}
